using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using HomePortal.Models;

namespace HomePortal.Controllers
{

    public class PostController : Controller
    {
        private readonly ITagRepository tagRepository;
        private readonly IPostRepository postRepository;
        private readonly ICommentRepository commentRepository;

        // If you are using Dependency Injection, you can delete the following constructor
        public PostController()
            : this(new TagRepository(), new PostRepository(), new CommentRepository())
        {
        }

        public PostController(ITagRepository tagRepository, IPostRepository postRepository,
                              ICommentRepository commentRepository)
        {
            this.tagRepository = tagRepository;
            this.postRepository = postRepository;
            this.commentRepository = commentRepository;
        }

        [Authorize(Users = "admin")]
        public ViewResult Index()
        {
            return View(postRepository.AllIncluding(post => post.Comments, post => post.Tags));
        }


        public ActionResult All()
        {
            return View(postRepository.AllIncluding(post => post.Comments, post => post.Tags).Where(post => post.Visible).OrderByDescending(o=>o.ID));
        }

        public ActionResult LastPosts()
        {
            IEnumerable<Post> lastPosts = new Post[0];//postRepository.AllIncluding().Where(post => post.Visible).OrderByDescending(x => x.ID).Take(5);
            return View(lastPosts);
        }

        public ViewResult Details(int id)
        {
            return View(postRepository.Find(id));
        }

        [ValidateInput(false)]
        [Authorize(Users = "admin")]
        public ActionResult Create()
        {
            ViewBag.PossibleTags = tagRepository.All;
            return View();
        }

        [Authorize(Users = "admin")]
        [HttpPost]
        public ActionResult Create(Post post)
        {
            if (ModelState.IsValid)
            {
                postRepository.InsertOrUpdate(post);
                postRepository.Save();
                return RedirectToAction("Index");
            }
            ViewBag.PossibleTags = tagRepository.All;
            return View();
        }

        [Authorize(Users = "admin")]
        public ActionResult Edit(int id)
        {
            ViewBag.PossibleTags = tagRepository.All;
            return View(postRepository.Find(id));
        }

        [Authorize(Users = "admin")]
        [HttpPost]
        public ActionResult Edit(Post post)
        {
            if (ModelState.IsValid)
            {
                postRepository.InsertOrUpdate(post);
                postRepository.Save();
                return RedirectToAction("Index");
            }
            ViewBag.PossibleTags = tagRepository.All;
            return View();
        }

        [Authorize(Users = "admin")]
        public ActionResult Delete(int id)
        {
            return View(postRepository.Find(id));
        }

        [Authorize(Users = "admin")]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            postRepository.Delete(id);
            postRepository.Save();

            return RedirectToAction("Index");
        }

        [Authorize]
        [HttpPost]
        public ActionResult AddComment(int id, string messageText)
        {
            var comment = new Comment {PostID = id, CommentDate = DateTime.Now};
            if (!String.IsNullOrEmpty(messageText))
            {
                comment.CommentText = messageText;
                comment.UserName = User.Identity.Name;

                if (ModelState.IsValid)
                {
                    commentRepository.InsertOrUpdate(comment);
                    commentRepository.Save();
                    return RedirectToAction("/Details/" + id, "Post");
                }
                ViewBag.PossiblePosts = postRepository.All;
                return View("All");
            }
            return RedirectToAction("/Details/" + id, "Post");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                tagRepository.Dispose();
                postRepository.Dispose();
            }
            base.Dispose(disposing);
        }

        public ActionResult Search(int id)
        {
            return View("All",
                        postRepository.AllIncluding(post => post.Comments, post => post.Tags).Where(post => post.Visible)
                            .Where(x => x.TagID == id).OrderByDescending(o => o.ID));
        }

        public ActionResult SearchText(string searchText)
        {
            return View("SearchResults",
                        postRepository.AllIncluding().Where(post => post.Visible).Where(
                            x => x.Title.Contains(searchText) || x.MainContent.Contains(searchText)).OrderByDescending(o => o.ID));
        }

        public ActionResult Download(string fileName)
        {
            string path = Server.MapPath(@"\Content\Files\") + fileName;
            return File(path, "application/octet-stream", fileName);
        }
    }
}

